<?php


namespace app\admin\model;

use think\facade\Config;
use think\facade\Db;
use think\Model;
use think\facade\Cache;
class Base extends Model
{
    /**
     * @param $value
     * Desc: 创建时间获取器
     * User: 苏鹏
     * Date: 2023/9/19
     * Time: 15:52
     * @return int
     */
    public function getCreateTimeAttr($value)
    {
        return empty($value) ? '' : date('Y-m-d H:i:s', $value);
    }

    /**
     * @param $value
     * Desc:修改时间获取器
     * User: 苏鹏
     * Date: 2023/9/19
     * Time: 15:52
     * @return int
     */
    public function getUpdateTimeAttr($value)
    {
        return empty($value) ? '' : date('Y-m-d H:i:s', $value);
    }


    /**
     * Desc: 获取数据表表字段
     * return 'id,name,sex'
     * User: 苏鹏
     * Date: 2023/10/20
     * Time: 11:05
     * @return void
     */
    public function getTableFields(string $table_name):string
    {
        $table_name = strtolower(preg_replace('/(?!^)[A-Z]/', '_$0', $table_name));
        $prefix = Config::get('database.connections.mysql.prefix');
        $table_info = Db::getFields($prefix . $table_name);
        $field_array = array_column($table_info, 'name');
        $fields = implode(',', $field_array);
        return $fields;
    }

}